const app = getApp()
const audio = wx.createInnerAudioContext()
Page({
  data: {
    info: null,
    imgpath: '',
    src:
      'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',
    percent: 0,
    bgTime: '00:00',
    playTime: '00:00',
    isPlay: false,
    playClass: 'fa fa-fw fa-play play'
  },
  onLoad: function(options) {
    const page = this
    const dataid = options.id
    app.util.request({
      url: 'entry/wxapp/getspot',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      data: {
        dataid: dataid
      },
      success: function(res) {
        wx.hideLoading()
        console.log(res.data)
        if (res.data.code == 200) {
          console.log(res.data.data.all_img)
          res.data.data.all_img = JSON.parse(res.data.data.all_img)
          page.setData({
            info: res.data.data,
            imgpath: app.data.imgpath
          })
        } else {
          wx.showToast({
            title: '请求错误！',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      },
      fail: function() {
        wx.hideLoading()
      }
    })
    //console.log(JSON.parse(info));

    audio.src = this.data.src
    audio.onPlay(() => {
      console.log('开始播放')
    })
    audio.onError(res => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
    audio.onTimeUpdate(() => {
      console.log('当前时间', audio.currentTime)
      console.log('时长:', audio.duration)
      const bgTime = this.secondToDate(audio.duration)
      const playTime = this.secondToDate(audio.currentTime)
      const percent = audio.currentTime / audio.duration
      this.setData({
        percent: percent * 100,
        bgTime: bgTime,
        playTime: playTime
      })
    })
  },
  unLoad() {
    audio.destroyed()
  },
  //导航
  navigation: function(e) {
    console.log(e)
    console.log(e.currentTarget.dataset.info)
    //console.log(parseFloat(e.target.dataset.info.latitude));
    var info = e.currentTarget.dataset.info
    wx.getLocation({
      //获取当前经纬度
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度，官方提示bug: iOS 6.3.30 type 参数不生效，只会返回 wgs84 类型的坐标信息
      success: function(res) {
        wx.openLocation({
          //​使用微信内置地图查看位置。
          latitude: parseFloat(info.latitude), //要去的纬度-地址
          longitude: parseFloat(info.longitude), //要去的经度-地址
          name: info.scenic_spot_name,
          address: info.jianjie
        })
      }
    })
  },
  //打卡
  clock: function(e) {
    console.log('e')
    var page = this
    console.log(e.currentTarget.dataset.data.id)
    wx.navigateTo({
      url: '../clock/clock?spotid=' + e.currentTarget.dataset.data.id
    })
  },
  secondToDate(result) {
    var m = this.setDb(Math.floor((result / 60) % 60))
    var s = this.setDb(Math.floor(result % 60))

    return (result = m + ':' + s)
  },
  setDb(num) {
    //补零操作
    if (num < 10) {
      return '0' + num
    } else {
      return '' + num
    }
  },
  audioPlay() {
    if (!this.data.isPlay) {
      audio.play()
      this.setData({
        isPlay: true,
        playClass: 'fa fa-fw fa-pause play'
      })
    } else {
      audio.pause()
      this.setData({
        isPlay: false,
        playClass: 'fa fa-fw fa-play play'
      })
    }
  }
})
